/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.odb.bzk3.midp;

import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;

/**
 * 
 * @author daniel
 */
public class SquareFactory {

	static Mesh MakeXY(float x1, float y1, float x2, float y2, float z, int aColor) {
		return MakeXY((short) ((x1 / 255.0f) * (1 << 16)),
				(short) ((y1 / 255.0f) * (1 << 16)),
				(short) ((x2 / 255.0f) * (1 << 16)),
				(short) ((y2 / 255.0f) * (1 << 16)),
				(short) ((z / 255.0f) * (1 << 16)), aColor);
	}

	static Mesh MakeXY(short x1, short y1, short x2, short y2, short z,
			int aColor) {
		VertexArray va = new VertexArray(4, 3, 2);
		{
			short[] values = { x1, y1, z, x2, y1, z, x2, y2, z, x1, y2, z };
			va.set(0, 4, values);
		}
		VertexBuffer vb = new VertexBuffer();
		vb.setDefaultColor(aColor);
		vb.setPositions(va, 255.0f / (1 << 16), null);
		PolygonMode pm = new PolygonMode();
		pm.setCulling(PolygonMode.CULL_NONE);
		pm.setWinding(PolygonMode.WINDING_CCW);
		Appearance ap = new Appearance();
		ap.setPolygonMode(pm);

		int[] strip = { 0, 1, 3, 2 };
		int[] lengths = { 4 };
		TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

		return new Mesh(vb, tsa, ap);

	}

	static Mesh MakeYZ(float iY, float iZ, float f, float g, float h, int aColor) {
		return MakeYZ((short) ((iY / 255.0f) * (1 << 16)),
				(short) ((iZ / 255.0f) * (1 << 16)),
				(short) ((f / 255.0f) * (1 << 16)),
				(short) ((g / 255.0f) * (1 << 16)),
				(short) ((h / 255.0f) * (1 << 16)), aColor);
	}

	static Mesh MakeYZ(short y1, short z1, short y2, short z2, short x,
			int aColor) {
		VertexArray va = new VertexArray(4, 3, 2);
		{
			short[] values = { x, y1, z1, x, y1, z2, x, y2, z2, x, y2, z1 };
			va.set(0, 4, values);
		}
		VertexBuffer vb = new VertexBuffer();
		vb.setDefaultColor(aColor);
		vb.setPositions(va, 255.0f / (1 << 16), null);
		PolygonMode pm = new PolygonMode();
		pm.setCulling(PolygonMode.CULL_NONE);
		pm.setWinding(PolygonMode.WINDING_CCW);
		Appearance ap = new Appearance();
		ap.setPolygonMode(pm);

		int[] strip = { 0, 1, 3, 2 };
		int[] lengths = { 4 };
		TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

		return new Mesh(vb, tsa, ap);

	}

	static Mesh MakeXZ(float iX, float iZ, float f, float g, float h, int aColor) {
		return MakeXZ((short) ((iX / 255.0f) * (1 << 16)),
				(short) ((iZ / 255.0f) * (1 << 16)),
				(short) ((f / 255.0f) * (1 << 16)),
				(short) ((g / 255.0f) * (1 << 16)),
				(short) ((h / 255.0f) * (1 << 16)), aColor);
	}

	static Mesh MakeXZ(short x1, short z1, short x2, short z2, short y,
			int aColor) {
		VertexArray va = new VertexArray(4, 3, 2);
		{
			short[] values = { x1, y, z2, x2, y, z2, x2, y, z1, x1, y, z1 };
			va.set(0, 4, values);
		}
		VertexBuffer vb = new VertexBuffer();
		vb.setDefaultColor(aColor);
		vb.setPositions(va, 255.0f / (1 << 16), null);
		PolygonMode pm = new PolygonMode();
		pm.setCulling(PolygonMode.CULL_NONE);
		pm.setWinding(PolygonMode.WINDING_CCW);
		Appearance ap = new Appearance();
		ap.setPolygonMode(pm);

		int[] strip = { 0, 1, 3, 2 };
		int[] lengths = { 4 };
		TriangleStripArray tsa = new TriangleStripArray(strip, lengths);

		return new Mesh(vb, tsa, ap);

	}
}
